package logic

import (
	"context"
	"fmt"
	"mini/model/mysql"
	"mini/pkg"

	"mini/userrpc/internal/svc"
	"mini/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserRegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRegisterLogic {
	return &UserRegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserRegisterLogic) UserRegister(in *userrpc.UserRegisterRequest) (*userrpc.UserRegisterResponse, error) {
	// todo: add your logic here and delete this line
	password1 := pkg.Md5(in.Password)
	user := mysql.Users{
		Tel:      in.Tel,
		Password: password1,
	}

	err := mysql.DB.Debug().Model(&mysql.Users{}).Create(&user).Error
	if err != nil {
		return nil, fmt.Errorf("注册失败")
	}
	return &userrpc.UserRegisterResponse{Id: int64(user.ID)}, nil
}
